<% if @team.present? %>
    <%= form_for @task, :html => {:class => 'form-horizontal task-form', :multipart => true, :data => {:abide => true}} do |f| %>
        <div class="form-content form-below">
          <%= f.hidden_field :task_id, :value => @root_task.id if @root_task.present? %>
          <%= f.hidden_field :team_id, :value => @root_task.team_id if @root_task.present? %>
          <%= f.hidden_field :project_id, :value => @root_task.project_id if @root_task.present? %>
          <%= f.hidden_field :user_id, :value => current_user.id %>
          <%= build_validated_field('task', 'name', 'Title', 'Cannot be blank', '', 'text', @task.name) %>
          <%= build_field('task', 'description', 'Description', 'text_area', @task.description) %>
          <%#= build_validated_field('task', 'description', 'Description', 'Cannot be blank', '', 'text_area', @task.description) %>
          <% unless @root_task.present? %>
              <div class='form-row'>
                <div class='small-3 columns'>
                  <%= f.label :team_id, 'Team' %>
                </div>
                <div class='small-9 columns'>
                  <%= select_tag "task[team_id]", grouped_options_for_select(@grouped_teams, @team.id) %>
                  <%#= grouped_collection_select(:task, :team_id, @projects, @teams, :name, :id, :name, {}, {:class => "team-selector"}) %>
                </div>
              </div>
          <% end %>
          <%= build_validated_field('task', 'start_date', 'Start Date', 'Cannot be blank', '', 'date_field', @task.start_date) %>
          <%= build_validated_field('task', 'end_date', 'End Date', 'Cannot be blank', '', 'date_field', @task.end_date) %>
          <% if @task.status == 'completed' %>
              <div class='form-row'>
                <div class='small-3 columns'>
                  <%= f.label :status, 'Status' %>
                </div>
                <div class='small-9 columns'>
                  <%= select_tag('task[status]', options_for_select([['Pending', 'active'], ['Completed', 'completed']], @task.status)) %>
                </div>
              </div>
          <% end %>
          <%#= build_validated_select('task', 'project_id', 'Project','Should select at least one','',false,@task.project_id,Project.active.map{|x| [x.name,x.id]}) %>
          <%#= build_validated_select('task', 'team_id', 'Team','Should select at least one','',false,@task.team_id,Team.active.map{|x| [x.name,x.id]}) %>
          <div class="priority-selector">
            <%= build_validated_select('task', 'priority', 'Priority', 'Should select at least one', '', false, @task.priority, ['Low', 'Medium', 'High', 'Showstopper']) %>
          </div>
          <div class="assignees-select">
            <%= render :partial => 'teams/members' %>

            <%#= build_validated_select('task', 'user_ids', 'Assignees', 'Should select at least one', '', true, @task.user_ids, (@users.nil? ? [] : @users.map { |x| [x.name, x.id] })) %>
          </div>
          <!--<div class="field">-->
          <%#= f.label :tracker_id %>
          <br>
          <!--<%#= f.text_field :tracker_id %>-->
          <!--</div>-->
          <!--<div class="field">-->
          <%#= f.label :comments_count %>
          <br>
          <!--<%#= f.number_field :comments_count %>-->
          <!--</div>-->
        </div>
        <div class="small-12 columns form-action-up">
          <div class="title">Task</div>
          <div class="right">
            <%= f.submit nil, :class => 'button alert right', :disable_with => "Saving" %>
            <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                        tasks_path, :class => 'btn cancel-btn' %>
          </div>
        </div>
    <% end %>
<% else %>
    <div class="message">
      You have no access to a team inorder to assign a task.
    </div>
<% end %>

<script type="text/javascript">
    window.location = '#pane3'
    $('select').not('.select_add').select2({'width': '100%'});
    //    $('.date-input').datetimepicker();
    $('.team-selector').on("change", function () {
        load_assignees()
//        $.get('/teams/get_member_list?team_id=' + $(this).val())
    });

    //    $('.date-input').datetimepicker().on('onChangeDateTime', function () {load_assignees()});

    $('.date-input').datetimepicker({
        onSelectDate: function () {
            load_assignees();
        }
    });

    $('#task_team_id').bind("change", function () {
        load_assignees()
    });
    $('#task_start_date').bind("input", function () {
        load_assignees()
    });
    $('#task_end_date').bind("input", function () {
        load_assignees()
    });
    function load_assignees() {
        if ($('#task_team_id').val() == '' || $('#task_start_date').val() == '' || $('#task_end_date').val() == '') {
            $('.assignees-select').html('Choose end date, start date and team for the task');
        } else {
            $('.assignees-select').html('satisfied');
            $.get('/teams/get_member_list?team_id=' + $('#task_team_id').val() + '&start_date=' + $('#task_start_date').val() + '&end_date=' + $('#task_end_date').val() + "&task_id=<%= @task.try(&:id) %>")
        }
    }
</script>